package com.dhh.sky.activity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ArrayAdapter;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.dhh.sky.R;
import com.dhh.sky.util.Constant;
import com.dhh.sky.widget.SlideMenuFragment;
import com.dhh.sky.widget.TransitionDownloadAdapter;
import com.dhh.sky.widget.TransitionUploadAdapter;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;
import com.viewpagerindicator.TabPageIndicator;

public class TransferActivity extends SlidingFragmentActivity implements ActionBar.OnNavigationListener{
	private FragmentStatePagerAdapter adapter;
	private ViewPager pager;
	private TabPageIndicator indicator;
	private String sView = Constant.TRANSFER_VIEW_DOWNLOAD;
	private int sPosition = 0;
	private Context context;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);
		setBehindContentView(R.layout.menu_frame);
		setContentView(R.layout.transition_manage);
		
		getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame, new SlideMenuFragment()).commit();
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		
		context = getSupportActionBar().getThemedContext();
        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.transfer_catalog, R.layout.sherlock_spinner_item);
        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        getSupportActionBar().setListNavigationCallbacks(list, this);
        
        getSupportActionBar().setTitle("");
		getSupportActionBar().setHomeButtonEnabled(true);    
    	getSupportActionBar().setIcon(R.drawable.ic_app_menu);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);  
        
        Bundle data = this.getIntent().getExtras();
        if(data!=null){
        	sView = data.getString(Constant.PARAM_ITEM_NAME);
        	sPosition = data.getInt(Constant.PARAM_REQUEST);
        }
	}
	
	@Override
	public void onResume(){
		super.onResume();
		initView();
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
	        case android.R.id.home:     
	        	toggle();
				return true;
        }
    	
        return false;
    }
	
	public void initNavigationView(String view,int position){
		if(Constant.TRANSFER_VIEW_DOWNLOAD.equals(view)){
			adapter = new TransitionDownloadAdapter(this.getSupportFragmentManager(),this);
	        pager.setAdapter(adapter); 
	        indicator.setViewPager(pager); 
		}else{
			adapter = new TransitionUploadAdapter(this.getSupportFragmentManager(),this);
	        pager.setAdapter(adapter); 
	        indicator.setViewPager(pager); 
		}
		
		indicator.setCurrentItem(position);
        adapter.notifyDataSetChanged();
		indicator.notifyDataSetChanged();
	}
	
	private void initView(){
		pager = (ViewPager)findViewById(R.id.pager);
        indicator = (TabPageIndicator)findViewById(R.id.indicator);
        initNavigationView(sView,sPosition);
	}

	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		if(itemPosition==0){
			sView = Constant.TRANSFER_VIEW_DOWNLOAD;
		}else if(itemPosition==1){
			sView = Constant.TRANSFER_VIEW_UPLOAD;
		}
		
		sPosition = 0;
		new TabRefreshTask().execute(new Integer[]{0});
		return true;
	}
	
	public class TabRefreshTask extends AsyncTask<Integer, Integer, Boolean>{
		@Override
		protected Boolean doInBackground(Integer... params) {
			tabRefreshHandler.sendEmptyMessage(0);
			return true;
		}
	}
	
	private Handler tabRefreshHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
        	initNavigationView(sView,sPosition);
        }
    };
}
